package pswvclass.pswvandroid2.com.need;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 * Created by Administrator on 2018/11/6.
 */
public class Results extends Fragment implements View.OnClickListener {

    private String tag = "RiskResults";
    private int skysetid;
    protected static final int NO_ADD = 2;
    protected static final int RESULTS_SUCCESS = 0;
    protected static final int ERROR = 1;
    private TextView plane_warn1;
    private TextView plane_warn2;
    private TextView plane_warn_s1;
    private TextView plane_warn_s2;
    private TextView plane_warn_s3;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.pswv_need_results, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            skysetid = bundle.getInt("skysetid");
            plane_warn1 = (TextView) getView().findViewById(R.id.plane_warn1);
            plane_warn2 = (TextView) getView().findViewById(R.id.plane_warn2);
            plane_warn_s1 = (TextView) getView().findViewById(R.id.plane_warn_s1);
            plane_warn_s2 = (TextView) getView().findViewById(R.id.plane_warn_s2);
            plane_warn_s3 = (TextView) getView().findViewById(R.id.plane_warn_s3);
            load();
        } else {
            Log.v(tag, "sadsadasdsad");
        }
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RESULTS_SUCCESS:
                    try {
                        JSONObject data = (JSONObject) msg.obj;
                        JSONObject jsonObj = new JSONObject(data + "");
                        JSONObject jsonObj1 = jsonObj.getJSONObject("warn");
                        String startime = jsonObj1.getString("startime");
                        String advancetime = jsonObj1.getString("advancetime");
                        String s1 = jsonObj1.getString("s1");
                        String s2 = jsonObj1.getString("s2");
                        String s3 = jsonObj1.getString("s3");
                        plane_warn1.setText(startime);
                        plane_warn2.setText(advancetime);
                        plane_warn_s1.setText(s1);
                        plane_warn_s2.setText(s2);
                        plane_warn_s3.setText(s3);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case ERROR:
                    Toast.makeText(getContext(), "连接错误 ....", Toast.LENGTH_SHORT).show();
                    break;
                case NO_ADD:
                    Toast.makeText(getContext(), " 没有查询到....", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }

        }

        ;
    };

    public void load() {

        new Thread() {
            @Override
            public void run() {
                try {
                    HttpUtils hu = new HttpUtils();
                    String data2;
                    final String src = getResources().getString(R.string.src_mysql) + "pswv/api/skySetAction/getFruit";
                    final Map<String, String> map1 = new HashMap<String, String>();
                    map1.put("id", skysetid + "");
                    data2 = hu.submitPostData(src, map1, "utf-8");
                    JSONObject jsonObj = new JSONObject(data2);
                    int result = Integer.parseInt(jsonObj.getString("code"));
                    JSONObject jsonObj1 = jsonObj.getJSONObject("response");
                    if (result == 0) {
                        Message msg = Message.obtain();
                        msg.obj = jsonObj1;
                        msg.what = RESULTS_SUCCESS;
                        mHandler.sendMessage(msg);
                    } else {
                        Message msg = Message.obtain();
                        msg.what = NO_ADD;
                        mHandler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = Message.obtain();
                    msg.what = ERROR;
                    mHandler.sendMessage(msg);
                }
            }
        }.start();
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            default:
                break;
        }
    }
}
